import { Color } from 'https://unpkg.com/three/build/three.module.js';
import { initShaders } from '../jsm/Utils.js';
import lib from '../jsm/ThreeLib.js';
import Poly from '../jsm/Poly.js'
import Sky from '../jsm/Sky.js'
import ShapeGeo from '../jsm/ShapeGeo.js';

window.onload = function () {

    const backColor = new Color("rgba(0,0,0,1)");

    const canvas = document.getElementById("canvas");
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;

    const gl = canvas.getContext("webgl");
    //开启颜色合成,才会有透明度的感觉
    gl.enable(gl.BLEND);
    gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);

    //初始化着色器
    initShaders(gl,
        document.querySelector("#vertexShader").innerText,
        document.querySelector("#fragmentShader").innerText);
    lib.clearCanvas(gl, new Color("rgba(0,0,0,1)"), 1);

    var u_tran = gl.getUniformLocation(gl.program, 'u_translation');
    var u_cosB = gl.getUniformLocation(gl.program, 'u_cosB');
    var u_sinB = gl.getUniformLocation(gl.program, 'u_sinB');
    var u_scale = gl.getUniformLocation(gl.program, 'u_scale');



    let poly = new Poly({
        gl: gl,
        vertices: [
            -0.2, 0,
            0.2, 0,
            0.0, 0.3,
        ]
    })

    poly.draw(["TRIANGLES"]);
    let y = 0;
    let angle = 0.0;
    let scale = 1;
    let judge = true;
    !(function ani() {
        if (y >= 1) {
            y = -1.3;
        }

        gl.uniform4fv(u_tran, [0, y, 0, 0]);

        gl.uniform1f(u_cosB, Math.cos(angle));
        gl.uniform1f(u_sinB, Math.sin(angle));

        gl.uniform1f(u_scale, Math.sin(angle) + 1);

        poly.draw(["TRIANGLES"]);
        // y += 0.01;
        angle += 0.01;

        requestAnimationFrame(ani);
    })();


}